import time
import hmac
import hashlib
import base64
import urllib.parse

import requests

timestamp = str(round(time.time() * 1000))
secret = 'SEC68af196b040a65d138e3209e44fd4b085b1d4db93b2aaf7bb058c8de5d0b29c1'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)


post_url = f'https://oapi.dingtalk.com/robot/send?' \
           f'access_token=36889f721b13044e3ca82346efd8a40d7c2356e8fcf2ffbd9d82fdad92e7da7b' \
           f'&timestamp={timestamp}&sign={sign}'

"""

post_url = 'https://oapi.dingtalk.com/robot/send?' \
           'access_token=36889f721b13044e3ca82346efd8a40d7c2356e8fcf2ffbd9d82fdad92e7da7b'
"""

# Webhook:
# https://oapi.dingtalk.com/robot/send?access_token=36889f721b13044e3ca82346efd8a40d7c2356e8fcf2ffbd9d82fdad92e7da7b
# https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&timestamp=XXX&sign=XXX

headers = {"Content-Type": "application/json"}
msg = '{"msgtype": "text","text": {"content":"我就是我, 是不一样的烟火1111"}}'
resp = requests.post(post_url, data=msg.encode('utf-8'), headers=headers)
print(resp)
